package config

import (
	"cyiris/core/mq"
	"cyiris/core/mysql"
	"cyiris/core/zaplog"
	"flag"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

var configPath = flag.String("conf", "conf/setting.yml", "Path to a configuration file")

type Config struct {
	Debug       bool                    `yaml:"debug"`
	ServerHost  string                  `yaml:"server_host"`
	LogConfig   zaplog.LogConfig        `yaml:"log"`
	Mysql       map[string]mysql.Config `yaml:"mysql"`
	Redis       map[string]RedisConfig  `yaml:"redis"`
	MqCfg       mq.MqCfg                `yaml:"mq_addr"`
	QuereCfg    []mq.QueueCfg           `yaml:"mq_queue"`
	ExchangeCfg []mq.ExchangeCfg        `yaml:"mq_exchange"`
}

type RedisConfig struct {
	Addr     string `yaml:"addr"`
	Passwd   string `yaml:"passwd"`
	Db       int    `yaml:"db"`
	PoolSize int    `yaml:"pool_size"`
}

func NewConfig() (cfg *Config, err error) {
	// 加载配置文件
	b, err := ioutil.ReadFile(*configPath)
	if err != nil {
		return nil, err
	}

	// 解析配置文件
	cfg = &Config{}
	if err = yaml.Unmarshal(b, cfg); err != nil {
		return nil, err
	}

	return cfg, nil
}
